﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class mwc_admin_controls_house : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        initPage();
        loadHouseCategory();
        loadHouseCategoryInList();
        loadHouseList();
    }
    protected void initPage() { 
        string task = Request.QueryString["task"];
        switch (task) { 
            case "add":
                pn_listhouse.Visible = false;
                pn_housedetail.Visible = true;
                break;

            case "edit":
                loadEditHouse();
                pn_listhouse.Visible = false;
                pn_housedetail.Visible = true;
                break;
            default:
                pn_listhouse.Visible = true;
                pn_housedetail.Visible = false;
                break;
        }
    }
    protected void loadHouseCategory() {
        string query = "SELECT * FROM mwc_housecategory";
        SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
        sc.Open();
        SqlDataAdapter sda = new SqlDataAdapter(query,sc);
        DataSet ds = new DataSet("housecategory");
        sda.Fill(ds);
        house_category.DataSource = ds.Tables[0];
        house_category.DataTextField = "catname";
        house_category.DataValueField = "id";
        house_category.DataBind();
        sc.Close();
    }
    protected void loadHouseCategoryInList() {
        string query = "SELECT * FROM mwc_housecategory";
        SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
        sc.Open();
        SqlDataAdapter sda = new SqlDataAdapter(query, sc);
        DataSet ds = new DataSet("housecategory");
        sda.Fill(ds);

        ddl_housecategory.DataSource = ds.Tables[0];
        ddl_housecategory.DataTextField = "catname";
        ddl_housecategory.DataValueField = "id";
        ddl_housecategory.DataBind();
        sc.Close();

        ListItem li = new ListItem();
        li.Text = "All";
        li.Value = "0";
        li.Selected = true;
        ddl_housecategory.Items.Add(li);
        sc.Close();
    }
    protected void uploadComplete(object sender, AsyncFileUploadEventArgs e) {
        if (AsyncFileUpload1.HasFile)
        {
            AsyncFileUpload1.SaveAs(ConfigurationSettings.AppSettings["upload_path"] + "house/" + AsyncFileUpload1.FileName);
            //loadFile("");
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "alert('hello world!');");
        }
    }
    protected void saveHouse(object sender, EventArgs e) { 
        string task = Request.QueryString["task"];
        switch (task) {
            case "add":
                addHouse();
                break;
            case "edit":
                break;
            default:
                break;
        }
    }
    protected void addHouse() {

        string catid = house_category.SelectedValue.ToString();
        string image = ConfigurationSettings.AppSettings["image_root_path"] + "house/" + AsyncFileUpload1.FileName;
        string sale_lease = house_sale_rent.SelectedValue.ToString();
        string enable = (house_enable.Checked == true) ? "1" : "0";

        string query = "INSERT INTO mwc_house(image,introtext,fulltext,address,postnumber,area,sale_lease,catid,saganr,enable) VALUES('"+image+"','"+house_introtext.Text+"','"+house_fulltext.Text+"','"+house_address.Text+"','"+house_postnumber.Text+"',"+house_areal.Text+","+sale_lease+","+catid+","+house_saganr.Text+","+enable+")";

        SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
        SqlCommand scmd = new SqlCommand(query,sc);
        sc.Open();
        scmd.ExecuteNonQuery();
        sc.Close();
        go2List();
    }
    protected void loadEditHouse() { 
        string id = Request.QueryString["id"];
        SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
        sc.Open();
        string query = "SELECT * FROM mwc_house WHERE id = " + id;
        SqlCommand scmd = new SqlCommand(query,sc);
        SqlDataReader sdr = scmd.ExecuteReader();
        while (sdr.Read()) {
            foreach (ListItem li in house_category.Items) {
                if (li.Value.ToString() == sdr["catid"].ToString())
                {
                    li.Selected = true;
                    
                }
            }
            
        }
    }
    protected void cancelHouse(object sender, EventArgs e) {
        go2List();
    }
    protected void go2List() {
        Response.Redirect("~/mwc_admin/Panel.aspx?option=house");
    }
    protected void loadHouseList() {
        string query = "SELECT * FROM mwc_house";
        try
        {
            SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
            sc.Open();
            SqlDataAdapter sda = new SqlDataAdapter(query, sc);
            DataSet ds = new DataSet("house");
            sda.Fill(ds);
            gvListHouse.DataSource = ds;
            gvListHouse.DataBind();
        }
        finally
        {
        }
    }
}
